package com.nanhai.buddhist.college.module.collegebase.framework.datapermission;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.google.common.collect.Sets;
import com.nanhai.buddhist.college.framework.common.enums.UserTypeEnum;
import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import com.nanhai.buddhist.college.framework.datapermission.core.rule.DataPermissionRule;
import com.nanhai.buddhist.college.framework.mybatis.core.dataobject.BaseDO;
import com.nanhai.buddhist.college.framework.mybatis.core.util.MyBatisUtils;
import com.nanhai.buddhist.college.framework.security.core.LoginUser;
import com.nanhai.buddhist.college.framework.security.core.util.SecurityFrameworkUtils;
import com.nanhai.buddhist.college.module.collegebase.api.collegeclass.CollegeClassApi;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.classroominfo.ClassroomInfoDO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.studentroll.StudentRollDO;
import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.InExpression;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;

//@AllArgsConstructor
//@Component
public class TeacherDataPermissionRule implements DataPermissionRule {


    private final Map<String, String> teacherColumns = new HashMap<>();

    private final Set<String> TABLE_NAMES = new HashSet<>();


    @Resource
    private  CollegeClassApi collegeClassApi;



    @PostConstruct
    public void initTable(){
        addTeacherColumn("t_student_roll","classes_id");
        addTeacherColumn("t_classroom_info","classes_id");
    }


    @Override
    public Set<String> getTableNames() {
        return TABLE_NAMES;
    }

    @Override
    public Expression getExpression(String tableName, Alias tableAlias) {

        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        if (loginUser == null) {
            return null;
        }
        // 只有管理员类型的用户，才进行数据权限的处理
        if (ObjectUtil.notEqual(loginUser.getUserType(), UserTypeEnum.TEACHER.getValue())) {
            return null;
        }

        // 根据教师的角色获取班级的信息

        CommonResult<List<String>> classesByTeacher = collegeClassApi.getClassesByTeacher(loginUser.getId());
        List<String> list = classesByTeacher.getData();

        if(list.isEmpty()){
            return new EqualsTo(null, null);
        }


        String columnName = "classes_id";
        return new InExpression(MyBatisUtils.buildColumn(tableName, tableAlias, columnName),
                new ExpressionList(CollectionUtils.convertList(list, StringValue::new)));
    }




    public void addTeacherColumn(Class<? extends BaseDO> entityClass, String columnName) {
        String tableName = TableInfoHelper.getTableInfo(entityClass).getTableName();
        addTeacherColumn(tableName, columnName);
    }


    public void addTeacherColumn(String tableName, String columnName) {
        teacherColumns.put(tableName, columnName);
        TABLE_NAMES.add(tableName);
    }
}
